/*
Copyright (c) 2019 Alibaba Group Holding Limited

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/
#include "stdio.h"
#include "vtimer.h"
#include "datatype.h"

#include "mailbox.h"
#include "my_timer.h"
#include "pmu.h"

//*******************************
//          USI0 UART
//*******************************
#define USI0_BADDR  0x50028000
#define USI1_BADDR  0x60028000
#define REG32(addr) *((volatile unsigned int *)(addr))

#define USI_CTRL    0x00
#define MODE_SEL    0x04
#define TX_FIFO     0x08
#define RX_FIFO     0x08
#define FIFO_STA    0x0c
#define CLK_DIV0    0x10
#define UART_CTRL   0x18
#define UART_STA    0x1c

typedef unsigned char u8;
typedef unsigned int u32;



/*
//delay 1ms
//tb中时钟频率为20MHz
//则其机器周期为12/20us
//则需要的周期个数为1667个
//转化成对应的十六进制约为0x1c8
void delay(void)
{
    uint32_t i;
    for(i=0;i<0x1c8;i++);
}
*/

int ree_main(void)
{
    int reg;
    int tx_empty;
    int rx_empty;
    int uart_busy;
    unsigned int cnt=0;
    u32 nor_cnt;
    u32 r2t_box_msg;
    u8 index;
    u32 data[20]={0};
    printf("ree core process : ree core starts successfully!\n");
    printf("ree core process : ree core has started, waiting for tee to complete the serial program\n");
    //等待TEE结束uart进程
    Polling_Tee_Process_Done();

    printf("=========================================\n");
    printf("the second process : ree core process start run!\n");
    printf("=========================================\n");

    //防止提前结束进程
    //*******************************
    //          USI0 UART
    //*******************************
    //sellect uart
    REG32(USI0_BADDR+MODE_SEL) = 0x0;

    //baud rate : 9600
    //CLK_DIV0 = (20M/(16*9600))-1 = 129
    REG32(USI0_BADDR+CLK_DIV0) = 0x81;

    //data_bits=8
    //stop_bits=1
    //parity disable
    REG32(USI0_BADDR+UART_CTRL) = 0x3;

    //enable uart
    //enable tx fifo
    REG32(USI0_BADDR+USI_CTRL) = 0x7;

    //*******************************
    //          USI1 UART
    //*******************************
    //sellect uart
    REG32(USI1_BADDR+MODE_SEL) = 0x0;

    //baud rate : 9600
    //CLK_DIV0 = (20M/(16*9600))-1 = 129
    REG32(USI1_BADDR+CLK_DIV0) = 0x81;

    //data_bits=8
    //stop_bits=1
    //parity disable
    REG32(USI1_BADDR+UART_CTRL) = 0x3;

    //enable uart
    //enable rx fifo
    REG32(USI1_BADDR+USI_CTRL) = 0xb;

    //*******************************
    //      USI0 UART send data
    //*******************************
    //send data
    printf("=========================================\n");
    printf("ree core process : usi0 uart transmit start\n");
    printf("ree core process : send_data[0] : 0x60\n");
    printf("ree core process : send_data[1] : 0x61\n");
    printf("ree core process : send_data[2] : 0x62\n");
    printf("ree core process : send_data[3] : 0x63\n");
    REG32(USI0_BADDR+TX_FIFO) = 0x60;
    REG32(USI0_BADDR+TX_FIFO) = 0x61;
    REG32(USI0_BADDR+TX_FIFO) = 0x62;
    REG32(USI0_BADDR+TX_FIFO) = 0x63;

    //wait untill
    //  tx_fifo empty and
    //  transmit is not busy
    do{
        reg = REG32(USI0_BADDR+FIFO_STA);
        tx_empty = reg & 0x1;

        reg = REG32(USI0_BADDR+UART_STA);
        uart_busy = reg & 0x1;
    } while(tx_empty==0 || uart_busy==1);

    printf("ree core process : usi0 uart transmit complete\n");
    printf("=========================================\n");

    //*******************************
    //     USI1 UART receive data
    //*******************************
    printf("=========================================\n");
    printf("ree core process : usi1 uart receive start\n");

    do{
        reg = REG32(USI1_BADDR+FIFO_STA);
        rx_empty = reg & 0x4;

        if(rx_empty==0){
            reg = REG32(USI1_BADDR+RX_FIFO);
            printf("ree core process : receive_data[%i] : 0x%x\n",cnt,reg);
            cnt++;
        }
    } while(cnt<4);

    printf("ree core process : usi1 uart receive complete\n");
    printf("=========================================\n");
    //*******************************
    //          MAILBOX
    //*******************************
    //测试正常通讯,告知TEE REE的串口任务已经完成
    printf("ree core process : ree uart process has done\n");
    printf("ree core process : witing tee close ree power\n");
    Ree_Read_Data_From_Tee(data,0,20);
    for(index = 0;index<20;index++)
      printf("ree core process : from tee data %x\r\n",data[index]);
    Send_Ree_process_Done_Flag();
    while(1);
    //由tee core结束进程
    sim_end();

}


int tee_main(void)
{
    int reg;
    int tx_empty;
    int rx_empty;
    int uart_busy;
    unsigned int cnt=0;
    unsigned int rec_data=0;
    u32 nor_cnt;
    u32 t2r_box_msg;
    u32 data[20]={0};
    u8 index;
    PMU_InitTypeDef pmu_initstruct;
    //用延时模拟其他安全程序
    for(index = 0;index < 20;index++)
    {
      data[index] = 0xaabbcc00 + index;
    }
      
    Tee_Send_Data2ree(data,0);
    printf("tee core process : running safety procedures\n");
    tee_timer_delay(0x400*50);
    //唤醒REE core
    printf("tee core process : tee core wakes up ree core\n");

    pmu_initstruct.pmu_set_iram_addr = 0x90000000;
    pmu_initstruct.pmu_set_iram_len  = 0x00010000;
    pmu_initstruct.pmu_iram_dec_en   = ENABLE;

    Init_Iram_Bus_Param(&pmu_initstruct);
    Weak_Up_Ree_Core(0x90000000);

    tee_timer_delay(0x400*5);//定时器延迟防止冲突
    //ree core占用串口完成之后开始启动tee进程
    printf("=========================================\n");
    printf("the first process : tee core process start run!\n");
    printf("=========================================\n");
    //*******************************
    //          USI0 UART
    //*******************************
    //sellect uart
    REG32(USI0_BADDR+MODE_SEL) = 0x0;

    //baud rate : 9600
    //CLK_DIV0 = (20M/(16*9600))-1 = 129
    REG32(USI0_BADDR+CLK_DIV0) = 0x81;

    //data_bits=8
    //stop_bits=1
    //parity disable
    REG32(USI0_BADDR+UART_CTRL) = 0x3;

    //enable uart
    //enable tx fifo
    REG32(USI0_BADDR+USI_CTRL) = 0x7;

    //*******************************
    //          USI1 UART
    //*******************************
    //sellect uart
    REG32(USI1_BADDR+MODE_SEL) = 0x0;

    //baud rate : 9600
    //CLK_DIV0 = (20M/(16*9600))-1 = 129
    REG32(USI1_BADDR+CLK_DIV0) = 0x81;

    //data_bits=8
    //stop_bits=1
    //parity disable
    REG32(USI1_BADDR+UART_CTRL) = 0x3;

    //enable uart
    //enable rx fifo
    REG32(USI1_BADDR+USI_CTRL) = 0xb;

    //*******************************
    //      USI0 UART send data
    //*******************************
    //send data
    printf("=========================================\n");
    printf("tee core process : usi0 uart transmit start\n");
    printf("tee core process : send_data[0] : 0x60\n");
    printf("tee core process : send_data[1] : 0x61\n");
    printf("tee core process : send_data[2] : 0x62\n");
    printf("tee core process : send_data[3] : 0x63\n");
    REG32(USI0_BADDR+TX_FIFO) = 0x60;
    REG32(USI0_BADDR+TX_FIFO) = 0x61;
    REG32(USI0_BADDR+TX_FIFO) = 0x62;
    REG32(USI0_BADDR+TX_FIFO) = 0x63;

    //wait untill
    //  tx_fifo empty and
    //  transmit is not busy
    do{
        reg = REG32(USI0_BADDR+FIFO_STA);
        tx_empty = reg & 0x1;

        reg = REG32(USI0_BADDR+UART_STA);
        uart_busy = reg & 0x1;
    } while(tx_empty==0 || uart_busy==1);

    printf("tee core process : usi0 uart transmit complete\n");
    printf("=========================================\n");

    //*******************************
    //     USI1 UART receive data
    //*******************************
    printf("=========================================\n");
    printf("tee core process : usi1 uart receive start\n");

    do{
        reg = REG32(USI1_BADDR+FIFO_STA);
        rx_empty = reg & 0x4;

        if(rx_empty==0){
            reg = REG32(USI1_BADDR+RX_FIFO);
            printf("tee core process : receive_data[%i] : 0x%x\n",cnt,reg);
            cnt++;
        }
    } while(cnt<4);

    printf("tee core process : usi1 uart receive complete\n");
    printf("=========================================\n");
    //*******************************
    //          MAILBOX
    //*******************************
    //测试正常通讯,告知REE TEE的串口任务已经完成
    Send_Tee_process_Done_Flag();
    printf("tee core process : tee uart process has done\n");
    Polling_Ree_Process_Done();

    printf("tee core process : close ree core\n");
    REG32(PMU_BASE+PMU_EN_REG) = 0x00000002;
    tee_timer_delay(0x400*50);//方便查看波形
    printf("tee core process : end test\n");
    sim_end();
}

